home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / CEGUISDK-0.4.1-VC6-Native.exe / {app} / include / renderers / OgreGUIRenderer / OgreCEGUIRenderer.h next >
Encoding:
C/C++ Source or Header  |  2005-11-26  |  14.9 KB  |  452 lines

  1. /************************************************************************
  2.     filename:     OgreCEGUIRenderer.h
  3.     created:    11/5/2004
  4.     author:        Paul D Turner
  5.     
  6.     purpose:    Interface for main Ogre GUI renderer class
  7. *************************************************************************/
  8. /*************************************************************************
  9.     Crazy Eddie's GUI System (http://www.cegui.org.uk)
  10.     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
  11.  
  12.     This library is free software; you can redistribute it and/or
  13.     modify it under the terms of the GNU Lesser General Public
  14.     License as published by the Free Software Foundation; either
  15.     version 2.1 of the License, or (at your option) any later version.
  16.  
  17.     This library is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20.     Lesser General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU Lesser General Public
  23.     License along with this library; if not, write to the Free Software
  24.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  25. *************************************************************************/
  26. /*************************************************************************
  27.     This file contains code that is specific to Ogre (http://www.ogre3d.org)
  28. *************************************************************************/
  29. #ifndef _OgreCEGUIRenderer_h_
  30. #define _OgreCEGUIRenderer_h_
  31.  
  32. #if defined( __WIN32__ ) || defined( _WIN32 )
  33. #   ifdef OGRE_GUIRENDERER_EXPORTS
  34. #       define OGRE_GUIRENDERER_API __declspec(dllexport)
  35. #   else
  36. #       define OGRE_GUIRENDERER_API __declspec(dllimport)
  37. #   endif
  38. #else
  39. #   define OGRE_GUIRENDERER_API
  40. #endif
  41.  
  42. #include <CEGUIBase.h>
  43. #include <CEGUIRenderer.h>
  44. #include <CEGUITexture.h>
  45.  
  46. #include <OgreRenderQueueListener.h>
  47. #include <OgreSceneManagerEnumerator.h>
  48.  
  49. // Start of CEGUI namespace section
  50. namespace CEGUI
  51. {
  52. /*************************************************************************
  53.     Forward refs
  54. *************************************************************************/
  55. class OgreCEGUITexture;
  56. class OgreCEGUIRenderer;
  57.  
  58.  
  59. /*!
  60. \brief
  61.     RenderQueueListener based class used to hook into the ogre rendering system
  62. */
  63. class CEGUIRQListener : public Ogre::RenderQueueListener
  64. {
  65. public:
  66.     CEGUIRQListener(OgreCEGUIRenderer* renderer, Ogre::RenderQueueGroupID queue_id, bool post_queue)
  67.     {
  68.         d_renderer        = renderer;
  69.         d_queue_id        = queue_id;
  70.         d_post_queue    = post_queue;
  71.     }
  72.  
  73.     virtual ~CEGUIRQListener() {}
  74.  
  75.     virtual void    renderQueueStarted(Ogre::RenderQueueGroupID id, bool& skipThisQueue);
  76.     virtual void    renderQueueEnded(Ogre::RenderQueueGroupID id, bool& repeatThisQueue);
  77.  
  78.     // methods for adjusting target queue settings
  79.     void    setTargetRenderQueue(Ogre::RenderQueueGroupID queue_id)        {d_queue_id = queue_id;}
  80.     void    setPostRenderQueue(bool post_queue)        {d_post_queue = post_queue;}
  81.  
  82. private:
  83.     /*************************************************************************
  84.         Implementation Data
  85.     *************************************************************************/
  86.     OgreCEGUIRenderer*                d_renderer;        //!< CEGUI renderer object for Ogre.
  87.     Ogre::RenderQueueGroupID    d_queue_id;        //!< ID of the queue that we are hooked into
  88.     bool                        d_post_queue;    //!< true if we render after everything else in our queue.
  89. };
  90.  
  91.  
  92. /*!
  93. \brief
  94.     Renderer class to interface with Ogre engine.
  95. */
  96. class OGRE_GUIRENDERER_API OgreCEGUIRenderer : public Renderer
  97. {
  98. public:
  99.     /*!
  100.     \brief
  101.         Constructor for renderer class that uses Ogre for rendering.
  102.  
  103.     \param window
  104.         Pointer to an Ogre::RenderWindow object.
  105.  
  106.     \param queue_id
  107.         Ogre::RenderQueueGroupID value that specifies where the GUI should appear in the ogre rendering output.
  108.  
  109.     \param post_queue
  110.         set to true to have GUI rendered after render queue \a queue_id, or false to have the GUI rendered before render queue
  111.         \a queue_id.
  112.  
  113.     \param max_quads
  114.         Obsolete.  Set to 0.
  115.  
  116.     \param scene_type
  117.         One of the Ogre::SceneType enumerated values specifying the scene manager to be targeted by the GUI renderer.
  118.     */
  119.     OgreCEGUIRenderer(Ogre::RenderWindow* window, Ogre::RenderQueueGroupID queue_id = Ogre::RENDER_QUEUE_OVERLAY, bool post_queue = false, uint max_quads = 0, Ogre::SceneType scene_type = Ogre::ST_GENERIC);
  120.  
  121.  
  122.     /*!
  123.     \brief
  124.         Constructor for renderer class that uses Ogre for rendering.
  125.  
  126.     \param window
  127.         Pointer to an Ogre::RenderWindow object.
  128.  
  129.     \param queue_id
  130.         Ogre::RenderQueueGroupID value that specifies where the GUI should appear in the ogre rendering output.
  131.  
  132.     \param post_queue
  133.         set to true to have GUI rendered after render queue \a queue_id, or false to have the GUI rendered before render queue
  134.         \a queue_id.
  135.  
  136.     \param max_quads
  137.         Obsolete.  Set to 0.
  138.  
  139.     \param scene_manager
  140.         Pointer to an Ogre::SceneManager object that is to be used for GUI rendering.
  141.     */
  142.     OgreCEGUIRenderer(Ogre::RenderWindow* window, Ogre::RenderQueueGroupID queue_id, bool post_queue, uint max_quads, Ogre::SceneManager* scene_manager);
  143.  
  144.  
  145.     /*!
  146.     \brief
  147.         Destructor for Ogre renderer.
  148.     */
  149.     virtual ~OgreCEGUIRenderer(void);
  150.  
  151.  
  152.  
  153.     // add's a quad to the list to be rendered
  154.     virtual    void    addQuad(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
  155.  
  156.     // perform final rendering for all queued renderable quads.
  157.     virtual    void    doRender(void);
  158.  
  159.     // clear the queue
  160.     virtual    void    clearRenderList(void);
  161.  
  162.  
  163.     /*!
  164.     \brief
  165.         Enable or disable the queueing of quads from this point on.
  166.  
  167.         This only affects queueing.  If queueing is turned off, any calls to addQuad will cause the quad to be rendered directly.  Note that
  168.         disabling queueing will not cause currently queued quads to be rendered, nor is the queue cleared - at any time the queue can still
  169.         be drawn by calling doRender, and the list can be cleared by calling clearRenderList.  Re-enabling the queue causes subsequent quads
  170.         to be added as if queueing had never been disabled.
  171.  
  172.     \param setting
  173.         true to enable queueing, or false to disable queueing (see notes above).
  174.  
  175.     \return
  176.         Nothing
  177.     */
  178.     virtual void    setQueueingEnabled(bool setting)        {d_queueing = setting;}
  179.  
  180.  
  181.     // create an empty texture
  182.     virtual    Texture*    createTexture(void);
  183.  
  184.     // create a texture and load it with the specified file.
  185.     virtual    Texture*    createTexture(const String& filename, const String& resourceGroup = "General");
  186.  
  187.     // create a texture and set it to the specified size
  188.     virtual    Texture*    createTexture(float size);
  189.  
  190.     // create an OGRE resource provider.
  191.     virtual ResourceProvider* createResourceProvider(void);
  192.  
  193.     // destroy the given texture
  194.     virtual    void        destroyTexture(Texture* texture);
  195.  
  196.     // destroy all textures still active
  197.     virtual void        destroyAllTextures(void);
  198.  
  199.  
  200.     /*!
  201.     \brief
  202.         Return whether queueing is enabled.
  203.  
  204.     \return
  205.         true if queueing is enabled, false if queueing is disabled.
  206.     */
  207.     virtual bool    isQueueingEnabled(void) const    {return d_queueing;}
  208.  
  209.  
  210.     /*!
  211.     \brief
  212.     Return the current width of the display in pixels
  213.  
  214.     \return
  215.     float value equal to the current width of the display in pixels.
  216.     */
  217.     virtual float    getWidth(void) const        {return d_display_area.getWidth();}
  218.  
  219.  
  220.     /*!
  221.     \brief
  222.     Return the current height of the display in pixels
  223.  
  224.     \return
  225.     float value equal to the current height of the display in pixels.
  226.     */
  227.     virtual float    getHeight(void) const        {return d_display_area.getHeight();}
  228.  
  229.  
  230.     /*!
  231.     \brief
  232.     Return the size of the display in pixels
  233.  
  234.     \return
  235.     Size object describing the dimensions of the current display.
  236.     */
  237.     virtual Size    getSize(void) const            {return d_display_area.getSize();}
  238.  
  239.  
  240.     /*!
  241.     \brief
  242.     Return a Rect describing the screen
  243.  
  244.     \return
  245.     A Rect object that describes the screen area.  Typically, the top-left values are always 0, and the size of the area described is
  246.     equal to the screen resolution.
  247.     */
  248.     virtual Rect    getRect(void) const            {return d_display_area;}
  249.  
  250.  
  251.     /*!
  252.     \brief
  253.         Return the maximum texture size available
  254.  
  255.     \return
  256.         Size of the maximum supported texture in pixels (textures are always assumed to be square)
  257.     */
  258.     virtual    uint    getMaxTextureSize(void) const        {return 2048;}        // TODO: Change to proper value
  259.  
  260.  
  261.     /*!
  262.     \brief
  263.         Return the horizontal display resolution dpi
  264.  
  265.     \return
  266.         horizontal resolution of the display in dpi.
  267.     */
  268.     virtual    uint    getHorzScreenDPI(void) const    {return 96;}
  269.  
  270.  
  271.     /*!
  272.     \brief
  273.         Return the vertical display resolution dpi
  274.  
  275.     \return
  276.         vertical resolution of the display in dpi.
  277.     */
  278.     virtual    uint    getVertScreenDPI(void) const    {return 96;}
  279.  
  280.  
  281.     /*!
  282.     \brief
  283.         Set the scene manager to be used for rendering the GUI.
  284.  
  285.         The GUI system will be unhooked from the current scene manager and attached to what ever
  286.         is specified here.
  287.  
  288.     \param scene_type
  289.         One of the Ogre::SceneType enumerated values indicating the new target Ogre::SceneManager to be
  290.         used for GUI rendering.
  291.  
  292.     \return
  293.         Nothing.
  294.     */
  295.     void    setTargetSceneManager(Ogre::SceneType scene_type);
  296.  
  297.  
  298.     /*!
  299.     \brief
  300.         Set the scene manager to be used for rendering the GUI.
  301.  
  302.         The GUI system will be unhooked from the current scene manager and attached to what ever
  303.         is specified here.
  304.  
  305.     \param scene_manager
  306.         Pointer to an Ogre::SceneManager object that is the new target Ogre::SceneManager to be
  307.         used for GUI rendering.
  308.  
  309.     \return
  310.         Nothing.
  311.     */
  312.     void    setTargetSceneManager(Ogre::SceneManager* scene_manager);
  313.  
  314.  
  315.     /*!
  316.     \brief
  317.         Set the target render queue for GUI rendering.
  318.  
  319.     \param queue_id
  320.         Ogre::RenderQueueGroupID value specifying the render queue that the GUI system should attach to.
  321.  
  322.     \param post_queue
  323.         - true to specify that the GUI should render after everything else in render queue \a queue_id.
  324.         - false to specify the GUI should render before everything else in render queue \a queue_id.
  325.  
  326.     \return
  327.         Nothing.
  328.     */
  329.     void    setTargetRenderQueue(Ogre::RenderQueueGroupID queue_id, bool post_queue);
  330.  
  331.  
  332.     /*!
  333.     \brief
  334.         Create a texture from an existing Ogre::TexturePtr object.
  335.  
  336.     \note
  337.         If you want to use an Ogre::RenderTexture (for putting rendered output onto Gui elements or other
  338.         advanced techniques), you can get the Ogre::TexturePtr to be used by calling Ogre::TextureManager::getByName()
  339.         passing the name returned from Ogre::RenderTexture::getName() (and casting the result as necessary).
  340.  
  341.     \param texture
  342.         pointer to an Ogre::TexturePtr object to be used as the basis for the new CEGUI::Texture
  343.  
  344.     \return
  345.         Pointer to the newly created CEGUI::TexturePtr object.
  346.     */
  347.     Texture*    createTexture(Ogre::TexturePtr& texture);
  348.  
  349.  
  350. private:
  351.     /************************************************************************
  352.         Implementation Constants
  353.     ************************************************************************/
  354.     static const size_t    VERTEX_PER_QUAD;                         //!< number of vertices per quad
  355.     static const size_t    VERTEX_PER_TRIANGLE;                     //!< number of vertices for a triangle
  356.     static const size_t    VERTEXBUFFER_INITIAL_CAPACITY;         //!< initial capacity of the allocated vertex buffer
  357.     static const size_t    UNDERUSED_FRAME_THRESHOLD;            //!< number of frames to wait before shrinking buffer
  358.  
  359.     /*************************************************************************
  360.         Implementation Structs & classes
  361.     *************************************************************************/
  362.     /*!
  363.     \brief
  364.         structure used for all vertices.
  365.     */
  366.     struct QuadVertex {
  367.         float x, y, z;            //!< The position for the vertex.
  368.         Ogre::RGBA diffuse;        //!< colour of the vertex
  369.         float tu1, tv1;            //!< texture coordinates
  370.     };
  371.  
  372.     /*!
  373.     \brief
  374.         structure holding details about a quad to be drawn
  375.     */
  376.     struct QuadInfo
  377.     {
  378.         Ogre::TexturePtr        texture;
  379.         Rect                position;
  380.         float                z;
  381.         Rect                texPosition;
  382.         uint32                topLeftCol;
  383.         uint32                topRightCol;
  384.         uint32                bottomLeftCol;
  385.         uint32                bottomRightCol;
  386.         
  387.         QuadSplitMode        splitMode;
  388.  
  389.         bool operator<(const QuadInfo& other) const
  390.         {
  391.             // this is intentionally reversed.
  392.             return z > other.z;
  393.         }
  394.     };
  395.  
  396.  
  397.     /*************************************************************************
  398.         Implementation Methods
  399.     *************************************************************************/
  400.     // setup states etc
  401.     void    initRenderStates(void);
  402.  
  403.     // sort quads list according to texture
  404.     void    sortQuads(void);
  405.  
  406.     // render a quad directly to the display
  407.     void    renderQuadDirect(const Rect& dest_rect, float z, const Texture* tex, const Rect& texture_rect, const ColourRect& colours, QuadSplitMode quad_split_mode);
  408.  
  409.     // convert colour value to whatever the Ogre render system is expecting.
  410.     uint32    colourToOgre(const colour& col) const;
  411.  
  412.     // perform main work of the constructor.  This does everything except the final hook into the render system.
  413.     void    constructor_impl(Ogre::RenderWindow* window, Ogre::RenderQueueGroupID queue_id, bool post_queue, uint max_quads);
  414.  
  415.  
  416.     /*************************************************************************
  417.         Implementation Data
  418.     *************************************************************************/
  419.     Rect                d_display_area;
  420.  
  421.     typedef std::multiset<QuadInfo>        QuadList;
  422.     QuadList d_quadlist;
  423.     bool     d_queueing;        //!< setting for queueing control.
  424.  
  425.     // Ogre specific bits.
  426.     Ogre::Root*                    d_ogre_root;        //!< pointer to the Ogre root object that we attach to
  427.     Ogre::RenderSystem*            d_render_sys;        //!< Pointer to the render system for Ogre.
  428.     Ogre::RenderQueueGroupID    d_queue_id;            //!< ID of the queue that we are hooked into
  429.     Ogre::TexturePtr            d_currTexture;        //!< currently set texture;
  430.     Ogre::RenderOperation        d_render_op;        //!< Ogre render operation we use to do our stuff.
  431.     Ogre::HardwareVertexBufferSharedPtr    d_buffer;    //!< vertex buffer to queue sprite rendering
  432.     size_t d_underused_framecount;                  //!< Number of frames elapsed since buffer utilization was above half the capacity
  433.     Ogre::RenderOperation        d_direct_render_op;        //!< Renderop for cursor
  434.     Ogre::HardwareVertexBufferSharedPtr    d_direct_buffer;    //!< Renderop for cursor
  435.     Ogre::SceneManager*            d_sceneMngr;        //!< The scene manager we are hooked into.
  436.     Ogre::LayerBlendModeEx        d_colourBlendMode;    //!< Controls colour blending mode used.
  437.     Ogre::LayerBlendModeEx        d_alphaBlendMode;    //!< Controls alpha blending mode used.
  438.  
  439.     CEGUIRQListener*            d_ourlistener;
  440.     bool                        d_post_queue;        //!< true if we render after everything else in our queue.
  441.     size_t                        d_bufferPos;        //!< index into buffer where next vertex should be put.
  442.     bool                        d_sorted;            //!< true when data in quad list is sorted.
  443.     Point                        d_texelOffset;        //!< Offset required for proper texel mapping.
  444.  
  445.     std::list<OgreCEGUITexture*> d_texturelist;        //!< List used to track textures.
  446. };
  447.  
  448. } // End of  CEGUI namespace section
  449.  
  450.  
  451. #endif    // end of guard _OgreCEGUIRenderer_h_
  452.